﻿package mortal.common.cd
{
    import com.gengine.core.frame.*;

    public class SecTimerViewData extends Object
    {
        private var _secTimer:SecTimer;
        private var _leftTime:int;
        private var _timerOutCallBack:Function;
        private const Second:int = 60;
        private const Minute:int = 60;
        private const Hour:int = 24;
        private var _parserList:Array;
        private var _call:Function;

        public function SecTimerViewData()
        {
            this._parserList = [{fun:this.dd, str:"dd"}, {fun:this.d, str:"d"}, {fun:this.HH, str:"HH"}, {fun:this.hh, str:"hh"}, {fun:this.h, str:"h"}, {fun:this.mm, str:"mm"}, {fun:this.m, str:"m"}, {fun:this.sss, str:"sss"}, {fun:this.ss, str:"ss"}, {fun:this.s, str:"s"}];
            return;
        }// end function

        public function changeCheckLabel(param1:String, param2:String) : void
        {
            var _loc_3:Object = null;
            for each (_loc_3 in this._parserList)
            {
                
                if (_loc_3["str"] == param1)
                {
                    _loc_3["str"] = param2;
                }
            }
            return;
        }// end function

        public function dd() : String
        {
            return (this.getDate() < 10 ? ("0") : ("")) + this.getDate();
        }// end function

        public function d() : String
        {
            return this.getDate().toString();
        }// end function

        public function HH() : String
        {
            var _loc_1:* = this.getDate() * 24 + this.getHours();
            return (_loc_1 < 10 ? ("0") : ("")) + _loc_1;
        }// end function

        public function hh() : String
        {
            return (this.getHours() < 10 ? ("0") : ("")) + this.getHours();
        }// end function

        public function h() : String
        {
            return this.getHours().toString();
        }// end function

        public function mm() : String
        {
            return (this.getMinutes() < 10 ? ("0") : ("")) + this.getMinutes();
        }// end function

        public function m() : String
        {
            return this.getMinutes().toString();
        }// end function

        public function sss() : String
        {
            return this._leftTime.toString();
        }// end function

        public function ss() : String
        {
            return (this.getSeconds() < 10 ? ("0") : ("")) + this.getSeconds();
        }// end function

        public function s() : String
        {
            return this.getSeconds().toString();
        }// end function

        public function getDate() : int
        {
            return Math.floor(this._leftTime / (this.Second * this.Minute * this.Hour));
        }// end function

        public function getHours() : int
        {
            return Math.floor(this._leftTime % (this.Second * this.Minute * this.Hour) / (this.Minute * this.Second));
        }// end function

        public function getMinutes() : int
        {
            return Math.floor(this._leftTime % (this.Second * this.Minute) / this.Second);
        }// end function

        public function getSeconds() : int
        {
            return this._leftTime % this.Second;
        }// end function

        private function onTimeChange(param1:SecTimer) : void
        {
            if (param1.repeatCount > int.MAX_VALUE / 2)
            {
                this._leftTime = param1.currentCount;
            }
            else
            {
                this._leftTime = param1.repeatCount >= 0 ? (param1.repeatCount) : (0);
            }
            if (this._call != null)
            {
                this._call.call();
            }
            return;
        }// end function

        private function onTimerComHandler(param1:SecTimer) : void
        {
            if (this._timerOutCallBack != null)
            {
                this._timerOutCallBack.call();
            }
            return;
        }// end function

        public function get leftTime() : int
        {
            return this._leftTime;
        }// end function

        public function tempChangeLeftTime(param1:int) : void
        {
            this._leftTime = param1;
            return;
        }// end function

        public function setLeftTime(param1:int, param2:Boolean = true) : void
        {
            this._leftTime = param1;
            if (this._leftTime < 0)
            {
                return;
            }
            if (!this._secTimer)
            {
                this._secTimer = new SecTimer(1, param1);
            }
            else
            {
                this._secTimer.reset();
                this._secTimer.repeatCount = param1;
            }
            if (param2)
            {
                this.start();
            }
            return;
        }// end function

        public function parse(param1:String) : String
        {
            var _loc_3:Object = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._parserList.length)
            {
                
                _loc_3 = this._parserList[_loc_2];
                param1 = param1.replace(_loc_3.str, (_loc_3.fun as Function).call());
                _loc_2++;
            }
            return param1;
        }// end function

        public function set caller(param1:Function) : void
        {
            this._call = param1;
            return;
        }// end function

        public function reset() : void
        {
            if (this._secTimer)
            {
                this._secTimer.reset();
            }
            return;
        }// end function

        public function stop() : void
        {
            if (this._secTimer)
            {
                this._secTimer.stop();
            }
            return;
        }// end function

        public function start() : void
        {
            if (this._secTimer)
            {
                this._secTimer.start();
                this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimeChange);
                this._secTimer.addListener(TimerType.COMPLETE, this.onTimerComHandler);
            }
            return;
        }// end function

        public function dispose() : void
        {
            this.stop();
            this._secTimer.dispose();
            this._call = null;
            this._timerOutCallBack = null;
            return;
        }// end function

        public function set timeOutCaller(param1:Function) : void
        {
            this._timerOutCallBack = param1;
            return;
        }// end function

        public function get running() : Boolean
        {
            return this._secTimer && this._secTimer.running;
        }// end function

    }
}
